<template>
	<view class="userpost" :class="[isDark?'dark':'', $store.state.AppStyle]" :style="{'background-color':isDark?'#1c1c1c':'#f6f6f6','min-height':isDark?'100vh':'auto'}">
		<view class="header" :style="[{height:CustomBar + 'px'}]">
			<view class="cu-bar" :class="isDark?'dark':'bg-white'" :style="{'height': CustomBar + 'px','padding-top':StatusBar + 'px','background-color':isDark?'#1c1c1c':'#ffffff'}">
				<view class="action" @tap="back">
					<text class="cuIcon-back"></text>
				</view>
				<view class="content text-bold" :style="[{top:StatusBar + 'px'}]">
					{{$t('forum.section.allcircles')}}
				</view>
				<view class="action">
				</view>
			</view>
		</view>
		<view :style="[{padding:NavBar + 'px 10px 0px 10px'}]"></view>
		<view class="data-box" v-for="(item,index) in sectionList" :key="index">
			<view class="cu-bar" :class="isDark?'dark':'bg-white'" :style="{'background-color':isDark?'#1c1c1c':'#ffffff'}">
				<view class="action data-box-title">
					{{item.name}}
				</view>
			</view>
			<view class="section-page">
				<view class="section-page-box"  v-for="(data,j) in item.subList" :key="j" >
					<view class="section-ico"  @tap="goSection(data.id)">
						<image :src="data.pic" mode="aspectFill"></image>
						
					</view>
					<view class="section-intro"  @tap="goSection(data.id)">
						<view class="section-item-tile">
							{{data.name}}
						</view>
						<view class="section-item-value">
							{{data.postNum + $t('global.post')}}
						</view>
					</view>
					<view class="section-page-btn">
						<block v-if="type==0">
							<view class="cu-btn bg-gradual-red"  @tap="data.isFollow=1,sectionFollow(data.id,1)" v-if="data.isFollow==0">
								<text class="cuIcon-add margin-right-xs"></text>{{$t('global.join')}}
							</view>
							<view class="cu-btn"  @tap="data.isFollow=0,sectionFollow(data.id,0)" v-if="data.isFollow==1">
								{{$t('global.exit')}}
							</view>
						</block>
						
						<view class="cu-btn bg-blue" @tap="setCur(data)"  v-if="type==1">
							{{$t('common.select')}}
						</view>
						<view class="cu-btn bg-gradual-orange"  @tap="addPost(data)"  v-if="type==2">
							<text class="cuIcon-add margin-right-xs"></text>{{$t('forum.section.posted')}}
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import { localStorage } from '../../js_sdk/mp-storage/mp-storage/index.js'
	import darkModeMixin from '@/utils/darkModeMixin.js'
	export default {
		mixins: [darkModeMixin],
		data() {
			return {
				StatusBar: this.StatusBar,
				CustomBar: this.CustomBar,
				NavBar:this.StatusBar +  this.CustomBar,
				AppStyle:this.$store.state.AppStyle,
				sectionList:[],
				moreText:$t('common.loadmoretxt'),
				page:1,
				isLoading:0,
				type:0,
				sectionid:0
			}
		},
		onLoad(res) {
			var that = this;
			if(localStorage.getItem('token')){
				that.token = localStorage.getItem('token');
			}
			// #ifdef APP-PLUS || MP
			that.NavBar = that.CustomBar;
			// #endif
			if(res.type){
				that.type = res.type;
			}
		},
		onShow(){
			var that = this;
			if(localStorage.getItem('token')){
				that.token = localStorage.getItem('token');
			}
			if(localStorage.getItem('sectionList')){
				that.sectionList = JSON.parse(localStorage.getItem('sectionList'));
			}
			// #ifdef APP-PLUS
			//plus.navigator.setStatusBarStyle("dark")
			// #endif
			that.getSectionList(false);
			that.page = 1;
			// 检查是否需要刷新数据
			that.checkAndRefreshData();
		},
		methods: {
			// 检查是否需要刷新数据
			checkAndRefreshData() {
				var that = this;
				// 检查板块帖子刷新标记
				var needRefreshPosts = localStorage.getItem('needRefreshPosts_' + that.sectionid);
				if (needRefreshPosts === 'true') {
					localStorage.removeItem('needRefreshPosts_' + that.sectionid);
					// 刷新板块列表数据
					that.getSectionList(false);
				}
			},
			back(){
				uni.navigateBack({
					delta: 1
				});
			},
			setCur(data){
				var that = this;
				localStorage.setItem('curSection',JSON.stringify(data));
				that.back();
			},
			getSectionList(isPage){
				var that = this;
				that.$Net.request({
					url: that.$API.sectionList(),
					data:{
						"limit":50,
						"page":1,
						"token":that.token
					},
					header:{
						'Content-Type':'application/x-www-form-urlencoded'
					},
					method: "get",
					dataType: 'json',
					success: function(res) {
						uni.stopPullDownRefresh()
						that.isLoad=0;
						if(res.data.code==1){
							var list = res.data.data;
							var parentList = [];
							for(var i in list){
								if(list[i].parent==0){
									list[i].subList = [];
									parentList.push(list[i]);
								}
							}
							for(var j in list){
								if(list[j].parent!=0){
									for(var p in parentList){
										if(list[j].parent == parentList[p].id){
											parentList[p].subList.push(list[j]);
										}
									}
								}
							}
							that.sectionList = parentList;
							localStorage.setItem('sectionList',JSON.stringify(that.sectionList));
						}
						var timer = setTimeout(function() {
							that.isLoading=1;
							clearTimeout('timer')
						}, 300)
					},
					fail: function(res) {
						uni.stopPullDownRefresh()
						that.moreText = that.$t('common.loadmoretxt');
						that.isLoad=0;
						var timer = setTimeout(function() {
							that.isLoading=1;
							clearTimeout('timer')
						}, 300)
					}
				})
			},
			goSection(id){
				var that = this;
				if(that.type!=0){
					return false
				}
				uni.navigateTo({
				    url: '/pages/forum/home?id='+id
				});
			},
			sectionFollow(id,type){
				var that = this;
				uni.showLoading({
					title: that.$t('common.loadmore')
				});
				var token = "";
				if(localStorage.getItem('userinfo')){
					var userInfo = JSON.parse(localStorage.getItem('userinfo'));
					token=userInfo.token;
				}
				var data = {
					"sectionId":id,
					"token":token,
					"type":type
				}
				that.$Net.request({
					url: that.$API.sectionFollow(),
					data:data,
					header:{
						'Content-Type':'application/x-www-form-urlencoded'
					},
					method: "get",
					dataType: 'json',
					success: function(res) {
						setTimeout(function () {
							uni.hideLoading();
						}, 1000);
						uni.showToast({
							title: res.data.msg,
							icon: 'none'
						})
						if(res.data.code==0){
							that.getSectionList();
						}
					},
					fail: function(res) {
						setTimeout(function () {
							uni.hideLoading();
						}, 1000);
						uni.showToast({
							title: that.$t('common.networkfail'),
							icon: 'none'
						})
					}
				})
			},
			addPost(data){
				var that = this;
				if(!localStorage.getItem('token')||localStorage.getItem('token')==""){
					uni.showToast({
						title: that.$t('common.plslogin'),
						icon: 'none'
					})
					return false;
				}
				uni.navigateTo({
					url: '/pages/edit/addPost?section='+data.name+"&sectionid="+data.id
				});
				// uni.navigateTo({
				// 	url: '/pages/forum/addPost?section='+data.name+"&sectionid="+data.id
				// });
			},
		}
	}
</script>